﻿function show_reserve(sched_id, movie_name, movie_time) {
	$(".movie-name").html(movie_name + "<br/>" + movie_time);
	var popup = $(".reservation-popup");
	popup.append("<input id='sched-id' type='hidden' name='sched-id' value='" + sched_id + "' />");
	var doc_height = $(window).height();
	var doc_width = $(window).width();
	var popup_height = popup.height();
	var popup_width = popup.width();
	var top = Math.max(0, doc_height / 2 - popup_height / 1.5);
	var left = Math.max(0, doc_width / 2 - popup_width / 2);
	popup.css("top", top + "px");
	popup.css("left", left + "px");
	popup.fadeIn();

	fill_seats(sched_id);

	return false;
}

function hide_reserve() {
	$(".reservation-popup").fadeOut("slow", function () {
		$(".reservation-popup .name-input").val("");
		$(".reservation-popup .phone-input").val("");
		$(".reservation-popup .seatcount-select").val("0");
		$(".reservation-popup .seat-table").html("<img src='Styles/images/loading.gif' />");
		$("#sched-id").remove();
	});
}

function validate() {
	var name = $(".reservation-popup .name-input").val();
	var phone = $(".reservation-popup .phone-input").val();
	var seat_count = $(".reservation-popup .seatcount-select").val();

	if (name == "" || phone == "" || seat_count == "")
		return false;
	return true;
}

function fill_seats(sched_id) {
	var ajax_data = "sched=" + sched_id;

	$.ajax({
		url: "seats.aspx",
		data: ajax_data,
		success: function (result) {
			$(".seat-table").html(result);
		},
		error: function (err1, err2, err3) {
			alert(err1 + " || " + err2 + " || " + err3);
		}
	});
}

function submit_reservation() {
	if (!validate()) {
		alert("Enter your name and phone");
		return;
	}

	var sched_id = $("#sched-id").val();
	var seats = $(".reservation-popup .seat-table td.seatMarked");
	var name = $(".reservation-popup .name-input").val();
	var phone = $(".reservation-popup .phone-input").val();

	if (seats.length < 0) {
		alert("Select your seat");
		return;
	}

	for (var i = 0; i < seats.length; ++i) {
		var ajax_data = "cmd=add&name=" + name + "&phone=" + phone + "&schedid=" + sched_id + "&seatid=" + $(seats[i]).find("input").val();

		$.ajax({
			data: ajax_data,
			url: "ajax/reserve.aspx",
			type: "get",
			beforeSend: function () {
				if ($(".reservation-popup .name-input").val("") != "") {
					$(".reservation-popup .name-input").val("");
					$(".reservation-popup .phone-input").val("");
					$(".reservation-popup .seat-table").html("<img src='Styles/images/loading.gif' />");
				}
			},
			success: function (result) {
				$(".reservation-popup .seat-table").html("<span class='msg'>" + result + "</span>");
			},
			error: function (err1, err2, err3) {
				alert(err1 + " || " + err2 + " || " + err3);
			}
		});
	}
}

function SeatClicked(td) {
	if ($(td).hasClass("seatFree"))
		$(td).toggleClass("seatMarked");
}